package com.obas.component.tree.treeclass {

   import flash.events.EventDispatcher;
   import flash.utils.getDefinitionByName;

   import mx.collections.ArrayCollection;
   import mx.events.PropertyChangeEvent;

   /**
    * The node always as an item in LoadingTree.
    */
   [Bindable]
   public class Node extends EventDispatcher {
      // Node type.
      private var _type:String;

      // Parent node type.
      private var _parentNode:Node;

      // If has const label.
      private var _hasConstLabel:Boolean = true;

      // Const lable.
      private var _constLabel:String;

      // Use labelField to display label.
      private var _labelField:String;

      // Icon class on tree's item.
      private var _icon:Class;

      // If the node is (inventory/aggregate/leaf)
      private var _nodeType:String;

      // If has entity type limit.
      private var _hasEntityType:Boolean = true;

      // Entity type.
      private var _entityType:String;

      // Entity object.
      private var _entity:Object;

      // Call dataservice to get children.
      private var _dataService:String;

      // The types of all children. Splice by ','.
      private var _childrenTypes:Array;

      // Children under the node.
      private var _children:ArrayCollection;

      // Pending children node for aggregation node which will not display in tree
      private var _pendingChildren:ArrayCollection;

      // If has been initialize. If have get children.
      private var _initialized:Boolean = false;

      // Whether could refresh.
      private var _enableRefresh:Boolean = true;

      // The amount of children.
      private var _childrenAmout:int = 0;

      // Whether has contextMenu.
      private var _hasContextMenu:Boolean = true;

      // The info of contextMenu.
      private var _contextMenu:String;

      // Rollup flag
      private var _useRollup:Boolean;

      /**
      * Setting and getting methods.
      */
      public function set type(value:String):void {
         this._type = value;
      }

      public function get type():String {
         return this._type;
      }

      public function get parentNode():Node {
         return this._parentNode;
      }

      public function set parentNode(value:Node):void {
         this._parentNode = value;
      }

      public function get hasConstLabel():Boolean {
         return this._hasConstLabel && this._hasConstLabel != "";
      }

      public function set constLabel(value:String):void {
         this._constLabel = value;
      }

      public function get constLabel():String {
         return this._constLabel;
      }

      public function get name():String {
         return entity != null ? entity[labelField] : "";
      }

      public function set labelField(value:String):void {
         this._labelField = value;
      }

      public function get labelField():String {
         return this._labelField;
      }

      public function set initialized(value:Boolean):void {
         this._initialized = value;
      }

      public function get initialized():Boolean {
         return this._initialized;
      }

      public function set childrenAmout(value:int):void {
         this._childrenAmout = value;
      }

      public function get childrenAmout():int {
         return this._childrenAmout;
      }

      public function set nodeType(value:String):void {
         this._nodeType = value;
      }

      public function get nodeType():String {
         return this._nodeType;
      }

      public function set entity(value:Object):void {
         this._entity = value;
      }

      public function get entity():Object {
         return this._entity;
      }

      public function set dataService(value:String):void {
         this._dataService = value;
      }

      public function get dataService():String {
         return this._dataService;
      }

      public function set children(value:ArrayCollection):void {
         this._children = value;
      }

      public function get children():ArrayCollection {
         return this._children;
      }

      public function set pendingChildren(value:ArrayCollection):void {
         this._pendingChildren = value;
      }

      public function get pendingChildren():ArrayCollection {
         return this._pendingChildren;
      }

      public function set enableRefresh(value:Boolean):void {
         this._enableRefresh = value;
      }

      public function get enableRefresh():Boolean {
         return this._enableRefresh;
      }

      public function set icon(value:Class):void {
         this._icon = value;
      }

      [Transient]
      public function get icon():Class {
         return this._icon;
      }

      public function get hasContextMenu():Boolean {
         return this._contextMenu && this._contextMenu != "";
      }

      public function set contextMenu(value:String):void {
         this._contextMenu = value;
      }

      public function get contextMenu():String {
         return this._contextMenu;
      }

      public function set useRollup(value:Boolean):void {
         this._useRollup = value;
      }

      public function get useRollup():Boolean {
         return this._useRollup;
      }

      /**
      * For clone.
      */
      public function clone():Node {
         var node:Node = new Node();
         node._children = this._children;
         node._childrenAmout = this._childrenAmout;
         node._childrenTypes = this._childrenTypes;
         node._constLabel = this._constLabel;
         node._contextMenu = this._contextMenu;
         node._dataService = this._dataService;
         node._enableRefresh = this._enableRefresh;
         node._entity = this._entity;
         node._entityType = this._entityType;
         node._hasConstLabel = this._hasConstLabel;
         node._hasContextMenu = this._hasContextMenu;
         node._hasEntityType = this._hasEntityType;
         node._icon = this._icon;
         node._initialized = this._initialized;
         node._labelField = this._labelField;
         node._nodeType = this._nodeType;
         node._parentNode = this._parentNode;
         node._type = this._type;
         return node;
      }
   }
}
